package extend_widget

import (
	"clientDemo/ui/fyne_ui/common"
	"clientDemo/ui/fyne_ui/widget"
	"clientDemo/ui/fyne_ui/widget/interface"
	"clientDemo/utils"
	"fyne.io/fyne/v2"
	"fyne.io/fyne/v2/canvas"
	"image/color"
)

var _ widgetcommon.IWidgetItem = (*Image)(nil)

const (
	borderColor = 0xFF0000FF // 红色
	borderWidth = 2
)

func init() {
	widget.RegWidget("WidgetImagic", "图片", NewImage)
	//widget.RegWidget("ImagicEmpty2", NewImage)
}

type Image struct {
	*WidgetContainer
	Image *canvas.Image
	*Action
	rootNode widgetcommon.IWidgetItem //父节点
	name     string
	scale    float32
	imgSize  fyne.Size
}

func NewImage(name string, skinInfo widgetcommon.IWidgetSkin, rootNode widgetcommon.IWidgetItem) widgetcommon.IWidgetItem {
	if skinInfo == nil {
		return nil
	}
	o := &canvas.Image{}
	o.FillMode = canvas.ImageFillContain
	_image := &Image{
		Image:  o,
		Action: &Action{},
		WidgetContainer: &WidgetContainer{
			Container: &Container{
				name: name,
			},
			Border: &canvas.Rectangle{
				FillColor:   color.Transparent,
				StrokeColor: color.RGBA{255, 0, 0, 255}, //黄色
				StrokeWidth: 2,
			},
		},
		rootNode: rootNode,
	}
	_image.WidgetContainer.ac = _image.Action
	_image.IUserInfo = common.NewWidgetUserInfo(name, skinInfo.GetSkinName(), _image)
	_image.WidgetContainer.SetObjects([]fyne.CanvasObject{_image.Image})
	_image.SetBase(_image, rootNode)
	_image.ExtendBaseWidget(_image)
	_image.name = name
	_image.scale = 1
	return _image
}
func (i *Image) SetProperty(key, value string, isProperty bool) {
	i.WidgetContainer.SetProperty(key, value, isProperty)
	if key == "ImagicTexture" {
		i.SetImagePath(value)
	}
	if key == "Scale" {
		i.scale = utils.ParseFloat32(value)
		newSize := fyne.NewSize(i.imgSize.Width*i.scale, i.imgSize.Height*i.scale)
		i.Image.SetMinSize(newSize)
	}
}
func (i *Image) SetImagePath(path string) {
	if i.Image.File == path {
		return
	}
	i.Image.File = path // 设置图片路径
	i.Image.Refresh()   // 刷新控件以显示新图片
	i.imgSize = fyne.NewSize(float32(i.Image.Image.Bounds().Dx()), float32(i.Image.Image.Bounds().Dy()))
}

func (i *Image) MinSize() fyne.Size {
	return i.Image.MinSize()
	//i.ExtendBaseWidget(i)
	//return i.BaseWidget.MinSize()
}

//func (i *Image) Move(pos fyne.Position) {
//	//i.Image.Move(pos)    // 这个不能设置不然图片是双倍移动
//	i.BaseWidget.Move(pos) // 移动 BaseWidget
//	i.Refresh()            // 刷新以更新显示
//}
//func (i *Image) Hide() {
//	i.BaseWidget.Hide()
//}

//func (i *Image) Resize(size fyne.Size) {
//	i.BaseWidget.Resize(size) // 这个会调用到imageWidgetRenderer.Layout
//}

//func (i *Image) Refresh() {
//	//i.Image.Refresh()
//	i.BaseWidget.Refresh()
//}
/*
// CreateRenderer 是 fyne.Widget 接口的一部分，用于创建控件的渲染器
func (i *Image) CreateRenderer() fyne.WidgetRenderer {
	i.BaseWidget.ExtendBaseWidget(i)
	i.Image.FillMode = canvas.ImageFillOriginal // 设置为1:1显示
	return &imageWidgetRenderer{
		objects: []fyne.CanvasObject{i.Image, i.Border},
	}
}
type imageWidgetRenderer struct {
	objects []fyne.CanvasObject
}

func (r *imageWidgetRenderer) Layout(size fyne.Size) {
	for _, obj := range r.objects {
		obj.Resize(size)
		obj.Move(fyne.NewPos(0, 0))
	}
}

// MinSize 是 WidgetRenderer 接口的一部分，用于返回控件的最小大小
func (r *imageWidgetRenderer) MinSize() fyne.Size {
	return r.objects[0].MinSize()
}

// Refresh 是 WidgetRenderer 接口的一部分，用于刷新控件
func (r *imageWidgetRenderer) Refresh() {
	for _, obj := range r.objects {
		obj.Refresh()
	}
}

// Destroy 是 WidgetRenderer 接口的一部分，用于销毁控件
func (r *imageWidgetRenderer) Destroy() {
}

func (r *imageWidgetRenderer) Objects() []fyne.CanvasObject {
	return r.objects
}
*/
